#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
#include "fun.h"

int main_menu()
{
        int choise = 0;
        
	printf("--------简易版电话簿管理系统--------\n");
	printf("\t 1.添加一个分组\n");
	printf("\t 2.修改一个分组\n");
	printf("\t 3.删除一个分组\n");
	printf("\t 4.查找一个分组\n");
	printf("\t 5.输出所有分组\n");
	printf("\t 0.退出系统\n");
	printf("------------------------------------\n");
	printf("请输入您要选择的操作\n");
	scanf("%d", &choise);
	return choise;
}

int lxr_menu()
{
	int choise = 0;
    
	printf("---------- 联系人目录 ----------\n");
	printf("\t 1.添加新的联系人\n");
	printf("\t 2.修改一个联系人\n");
	printf("\t 3.删除一个联系人\n");
	printf("\t 4.查找一个联系人\n");
	printf("\t 5.移动一个联系人\n");
	printf("\t 6.输出所有联系人\n");
	printf("\t 0.返回上级菜单\n");
	printf("-------------------------------\n");
	printf("请输入您要选择的操作\n");
	scanf("%d", &choise);
	return choise;
}

void up_down_menu()
{
	putchar('\n');
	printf("---------- 上下浏览目录 ----------\n");
	printf("\t 1.显示上一个联系人\n");
	printf("\t 2.显示下一个联系人\n");
	printf("\t 0.返回上级菜单\n");
	printf("请输入您要选择的操作\n");
}

void up_and_down(LXR * pload)
{
	int select;

	while(1)
	{
		up_down_menu();
		scanf("%d", &select);
		if(select == 1)
		{
			pload = pload->prev;
			if(pload->prev != NULL)
			{
				printf("\t姓名\t\t电话号码\n");
				print_one_people(pload);
			}
			else
			{
				pload = pload->next;
				printf("%s 已经是当前分组的第一人\n\n", pload->name);
			}	
		}
		else if(select == 2)
		{
			if(pload->next != NULL)
			{
				pload = pload->next;
				printf("\t姓名\t\t电话号码\n");
				print_one_people(pload);
			}
			else
			{
				printf("%s 已经是当前分组的最后一人\n\n", pload->name);
			}	
		}	
		else
		{
			break;
		}		
	}
}

void menu_funG(GROUP * head, int choise)
{
	GROUP * load = NULL;
	GROUP * mload = NULL;
	LXR * pload = NULL;
	char groupName[20], newGroupName[20];
	char ch;
	int len;

	switch(choise)
	{
	case 1:
		printf("请输入新的分组名\n");
		scanf("%s", groupName);
		add_new_group(head, groupName);
		break;
	case 2:
		printf("请输入要修改的分组名\n");
		fflush(stdin);
		scanf("%s", groupName);
		load = sel_one_group(head, groupName);
		if(load != NULL)
		{
			printf("您确定要修改分组名吗?\n");
			ch = isSure();
			if(ch == 'y' || ch == 'Y')
			{
				printf("请输入新的分组名\n");
				fflush(stdin);
				scanf("%s", newGroupName);
			}
			strcpy(load->groupName, newGroupName);
			printf("修改成功!\n\n");
		}
		else
		{
			printf("%s 不存在！\n\n", groupName);
		}
		break;
	case 3:
		printf("请输入要删除的分组名\n");
		fflush(stdin);
		scanf("%s", groupName);
		load = sel_one_group(head, groupName);
		mload = sel_one_group(head, "weimingmin");
		if(load != NULL)
		{
			printf("您确定要删除该分组吗?\n");
			ch = isSure();
			if(ch == 'y' || ch == 'Y')
			{
				move_group(load, mload);
				del_one_group(load);
			}
			printf("删除成功！\n\n");
		}
		else
		{
			printf("%s 不存在！\n\n", groupName);
		}
		break;
	case 4:
		printf("请输入要查询的分组名\n");
		fflush(stdin);
		scanf("%s", groupName);
		load = sel_one_group(head, groupName);
		if(load != NULL)
		{
			while(1)
			{
				choise = lxr_menu();
				if(menu_funX(head, load, choise) == 0)
				{
					break;
				}
			}
		}
		else
		{
			printf("%s 不存在！\n\n", groupName);
		}
		break;
	case 5:
		len = get_group_length(head);
		if(len >= 2)
		{
			bubble_sort_group(head, len);
		}
		printf("------------------------------------\n");
		printf("\t分组名\n");
		print_all_group(head);
		printf("\n");
		break;
	case 0:
		save_linked_to_file(head);
		exit(0);
	}
}

int menu_funX(GROUP * head, GROUP * nload, int choise)
{
	GROUP * load = head->next;
	LXR * pload = NULL;
	char ch;
	char groupName[20];
	char name[20], newName[20];
	char phoneNum[20], newPhoneNum[20];
	int len, select;

	switch(choise)
	{
	case 1:
		while(1)
		{
			printf("请输入新联系人的姓名和电话号码\n");
			scanf("%s %s", name, phoneNum);
			if(is_check_phoneNum(phoneNum) == 1)
			{
				add_new_people(nload->h, name, phoneNum);
				break;				
			}
			printf("请输入正确的电话号码\n");
		}
		printf("添加成功！！\n\n");
		break;
	case 2:
		printf("请输入要修改人的姓名\n");
		fflush(stdin);
		scanf("%s", name);
		pload = sel_one_people(nload->h, name);
		modify_one_people(pload, name);
		break;
	case 3:
		printf("请输入要删除联系人的姓名\n");
		scanf("%s", name);
		pload = sel_one_people(nload->h, name);
		if(pload != NULL)
		{
			del_one_people(pload, name);
			printf("删除成功！！\n\n");	
		}
		else
		{
			printf("%s 不存在！！\n\n", name);
		}
		break;
	case 4:
		printf("请输入要查找联系人的姓名\n");
		scanf("%s", name);
		pload = sel_one_people(nload->h, name);
		if(pload != NULL)
		{
			printf("\t姓名\t\t电话号码\n");
			print_one_people(pload);
		}	
		else
		{
			printf("%s 不存在！！\n\n", name);
		}
		up_and_down(pload);
		break;
	case 5:
		printf("请输入要移动联系人的姓名\n");
		scanf("%s", name);
		pload = sel_one_people(nload->h, name);
		if(pload != NULL)
		{
			printf("请输入要移动到的分组名\n");
			scanf("%s", groupName);
			load = sel_one_group(head, groupName);
			if(load != NULL)
			{
				add_new_people(load->h, pload->name, pload->phoneNum);
				del_one_people(nload->h, name);
			}
			else
			{
				printf("%s 不存在！\n\n", groupName);
			}
		}
		else
		{
			printf("%s 不存在！！\n\n", name);
		}
		break;
	case 6:
		len = get_people_length(nload->h);
		if(len >= 2)
		{
			bubble_sort_people(nload->h, len);
		}
		printf("------------------------------------\n");
		printf("\t姓名\t\t电话号码\n");
		print_all_people(nload->h);
		printf("\n");
		break;
	case 0:
		return 0;
	}
	return 1;
}